package com.acme.bankapp.domain.bank;

import com.acme.bankapp.domain.bank.exceptions.NotEnoughFundsException;

public abstract class AbstractAccount implements Account {
	protected double balance;
	protected Client client;
	
	public AbstractAccount(double balance) {
		if (balance < 0) {
			throw new IllegalArgumentException(
					"Initial balance should be non-negative");
		}
		this.balance = balance;
	}

	public AbstractAccount() {
		this(0);
	}

	@Override
	public double getBalance() {
		return balance;
	}

	@Override
	public Client getClient() {
		return client;
	}

	@Override
	public void setClient(Client client) {
		this.client = client;
	}

	@Override
	public double deposit(double sum) throws IllegalArgumentException {
		if (sum <= 0) {
			throw new IllegalArgumentException(
					"Deposit amount should be positive");
		}
			
		balance += sum;

		return balance;
	}

	@Override
	public long decimalValue(){
		return Math.round(this.balance);
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((client == null) ? 0 : client.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AbstractAccount other = (AbstractAccount) obj;
		if (Double.doubleToLongBits(balance) != Double
				.doubleToLongBits(other.balance))
			return false;
		if (client == null) {
			if (other.client != null)
				return false;
		} else if (!client.equals(other.client))
			return false;
		return true;
	}

	abstract public void withdraw(double sum) throws NotEnoughFundsException;

	abstract public double maximumAmountToWithdraw();
}
